package tea.lib
{
	import flash.net.DatagramSocket;
	import flash.events.DatagramSocketDataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	public class UDPconnect {
		static private var _intance:UDPconnect;
		static public function get intance():UDPconnect {
			if (_intance == null) {
				_intance = new UDPconnect();
			}
			return _intance;
		}
		//==================================================================
		private const DEFAULTSERVERPORT:int = 23456;
		private const DEFAULTCLIENTPORT:int = 23457;
		private var ServerSocket:DatagramSocket;
		private var serverPort:int;
		private var clientList:Array;
		private var startTimer:uint;
		private var selfID:String;
		//==================================================================
		public function UDPconnect():void {
			ServerSocket = new DatagramSocket();
			serverPort = DEFAULTSERVERPORT;
			clientList = new Array();
			ServerSocket.addEventListener(DatagramSocketDataEvent.DATA, ServerReceived);
			ServerSocket.addEventListener(Event.CLOSE, ServerClose);
			ServerSocket.addEventListener(IOErrorEvent.IO_ERROR, ServerError);
		}
		private function ServerReceived(event:DatagramSocketDataEvent):void {
			trace('asd')
			//event.srcAddress,event.srcPort
			processData(checkRight(event.data));
		}
		private function ServerClose(event:Event):void {
			ServerSocket.close();
			ServerSocket = null;
			clientList = null;
		}
		private function ServerError(event:IOErrorEvent):void {
			trace("udp io error:", event.toString());
		}
		//==================================================================
		private function checkRight(__ba:ByteArray):ByteArray {
			if(__ba.length<16)return null;
			var _ba:ByteArray=new ByteArray();
			_ba.writeBytes(__ba,0,__ba.length);
			_ba.position=0;
			var num:uint=0;
			for (var i:uint = 0; i < _ba.length-4; i++) {
				num+=_ba.readUnsignedByte();
			}
			if(num == _ba.readUnsignedInt()){
				_ba.clear();
				_ba.writeBytes(__ba,0,__ba.length-4);
				return _ba
			}
			return null;
		}
		private function addCheck(__ba:ByteArray):ByteArray {
			var _ba:ByteArray=new ByteArray();
			_ba.writeBytes(__ba,0,_ba.length);
			_ba.position=0;
			var num:uint=0;
			for (var i:uint = 0; i < _ba.length; i++) {
				num+=_ba.readUnsignedByte();
			}
			_ba.writeUnsignedInt(num);
			return _ba;
		}
		private function send(_ba:ByteArray,_client:Object):void {
			ServerSocket.send(addCheck(_ba),0,0,_client.ip,_client.port);
		}
		private function processData(_ba:ByteArray):void {
			trace("data:",_ba.toString());
		}
		//==================================================================
		public function init(_port:int = 0, _id:String = ''):void {
			if (_id == '') {
				selfID = ((1 + Math.random()) * 10000000000).toString(32);
			}else {
				selfID = _id;
			}
			if (_port > 10000 && _port< 49000) {
				serverPort = _port;
			}
			ServerSocket.bind(serverPort);
			ServerSocket.receive();
			startTimer = getTimer();
		}
		
		/*
		send to all
		past to other
		aend to one
		
		responce ping
		
		ping 300
		responce ping 300
		
		waiting all input
		request again
		
		check connect quility
		*/
		/*
		byteArray.writeObject({password:password_txt.text});
		UDPSocket.send(byteArray,0,0,ip_txt.text,24688);

		varobj:Object=event.data.readObject();
		obj.srcAddress=event.srcAddress;
		obj.srcPort=event.srcPort;

		timer=newTimer(1000,1);
		timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerCompleted);
		timer.start();

		importcom.adobe.crypto.MD5;
		*/
	}
}